$(function(){
			searchFileList();
		})
	
		function format(time, format){
		    var t = new Date(time);
		    var tf = function(i){return (i < 10 ? '0' : '') + i};
		    return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a){
		        switch(a){
		            case 'yyyy':
		                return tf(t.getFullYear());
		                break;
		            case 'MM':
		                return tf(t.getMonth() + 1);
		                break;
		            case 'mm':
		                return tf(t.getMinutes());
		                break;
		            case 'dd':
		                return tf(t.getDate());
		                break;
		            case 'HH':
		                return tf(t.getHours());
		                break;
		            case 'ss':
		                return tf(t.getSeconds());
		                break;
		        }
		    })
		}
		
		function initTable() {
			var table = $('#fileTable').dataTable({			        
				 "columnDefs":[{
					 searchable:true
		       }],             
				"oLanguage" : { // 汉化
					"sProcessing" : "正在加载数据...",
					"sLengthMenu" : "_MENU_记录/页 ",
					"sZeroRecords" : "没有您要搜索的内容",
					"sInfo" : "_START_ 至 _END_  ，共 _TOTAL_ 项",
					"sInfoEmpty" : "记录数为0",
					"sInfoFiltered" : "(全部记录数 _MAX_  条)",
					"sInfoPostFix" : "",
					"sSearch" : "查找&nbsp;",
					"sUrl" : "",
					"oPaginate" : {
						"sFirst" : "第一页",
						"sPrevious" : " 上一页 ",
						"sNext" : " 下一页 ",
						"sLast" : " 最后一页 "
					}
				}
			});
		}
	
		function searchFileList(){
			var c_name = $("#c_name option:selected").val();
			$('#loading').css('display','block');
	    	$('#fileListDiv').css('display','none');
	    	
			$.ajax({
				type : "POST",
				url : "searchFileList",
				data : {'c_name':c_name},
				error : function(data) {
					$('#loading').css('display','none');
					$('#alertModal').modal('show')
					document.getElementById("alertMessage").innerHTML = "Connection error！";
				},
				success:function(data){
					//console.log(data);
					if(data == "error"){
						$('#alertModal').modal('show')
						document.getElementById("alertMessage").innerHTML = "Connection error！";
					}else{
						$("#fileListDiv").html("");
						
						var bodyStr = '<table class="table table-striped table-bordered table-hover dataTables-example text-center" id="fileTable"><thead><tr><th class="text-center">序号</th><th class="text-center">链接前缀</th>'
										+'<th class="text-center">起始编号</th><th class="text-center">截止编号</th><th class="text-center">产码时间</th><th class="text-center">产码数量</th><th class="text-center">操作</th></tr></thead><tbody>';
	                    
	                    for(var i=0;i<data.length;i++){
		                    var record = data[i];
		                    var url = record.url==null?"":record.url;
		                    var start_id = record.start_id==null?"":record.start_id+1;
		                    var end_id = record.end_id==null?"":record.end_id;
		                    var number = record.number==null?"":record.number;
		                    var btn = '<button type="button" class="btn btn-primary" onclick="getZipFile()">下载</button>';
	                    	var date = format((record.create_time==null?"":record.create_time),"yyyy-MM-dd HH:mm:ss");
	                    	bodyStr += '<tr><td>'+(i+1)+'</td><td>'+url+'</td><td>'+start_id+'</td><td>'+end_id+'</td><td>'+date+'</td><td>'+number
	                    				+'</td><td><button type="button" class="btn btn-primary" onclick="showModel('+record.id+')">下载文件</button></td></tr>';
	                    }
	                    bodyStr += '</tbody></table>';
	                    $("#fileListDiv").html(bodyStr);                     
	        			initTable();
	        			
	        			$('#loading').css('display','none');
						$('#fileListDiv').css('display','block');
					}
				}
			})
		}		
		
		function showModel(id){
			var index = layer.load(1, {
				shadeClose : false,
				shade: [0.5,'#D3D3D3'] //0.1透明度的白色背景
			});
			$("<div id = 'background'>").appendTo(window.parent.document.body).css({//添加一个遮罩
				"width" : "100%",
				"z-index" : "99999",
				"height" : "100%",
				"background-image" : "url('../../img/null.gif')",
				"position" : "absolute",
				"left" : "0px",
				"top" : "0px"
			});
			window.onbeforeunload = function(){
				return "当前有任务在执行,你确定要离开这个页面吗";
			}
			
			$.ajax({
				type : "POST",
				url : "getZipFile",
				data : {'id':id},
				timeout : 1000*60*3,//超时时间3min
				error : function(data) {
					$("#background",window.parent.document).remove();//清除遮罩
					window.onbeforeunload = null;
					layer.close(index);
					layer.msg('Connection error！');
				},
				success:function(data){
					$("#background",window.parent.document).remove();//清除遮罩
					window.onbeforeunload = null;
					layer.close(index);
					var reg = /[.zip]/;
					if(reg.test(data)){
						window.location.href = ctx+"/page/download.jsp?fileName="+data;	
						layer.msg('下载完成');
					}else{
						layer.msg('Connection error！');
					}
				}
			})
		}